/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgp;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author jstrang3
 */
public class BGA {

    ArrayList<ArrayList<ArrayList<Message>>> messageList;       // Tree Representation
    private int rounds;
    private int numGenerals;
    private int commander;

    public BGA(ArrayList<General> g) {
        messageList = new ArrayList<ArrayList<ArrayList<Message>>>();
        numGenerals = g.size();
        rounds = 1;
        for (int i = 0; i < g.size(); i++) {
            if (g.get(i).getType().equals(ByzantineModel.COMMANDER) || g.get(i).getType().equals(ByzantineModel.COMMANDER_REBEL)) {
                commander = i;
            }
            if (g.get(i).getType().equals(ByzantineModel.COMMANDER_REBEL) || g.get(i).getType().equals(ByzantineModel.GENERAL_REBEL)) {
                rounds++;

            }
        }
        for (int i = 0; i < numGenerals; i++) {
            messageList.add(new ArrayList<ArrayList<Message>>());
            for (int j = 0; j < rounds; j++) {
                messageList.get(i).add(new ArrayList<Message>());
                if (j == 0) {
                    messageList.get(i).get(j).add(new Message("?", Integer.toString(g.get(commander).getID()), "?"));
                }
                else if (i != commander){
                    for (int k = 0; k < numGenerals - j; k++) {
                        messageList.get(i).get(j).add(new Message("?", Integer.toString(g.get(commander).getID()), "?"));
                    }
                }
            }
        }
    }

    public ArrayList<Message> getMessageList(int generalIndex, int roundNumber){
        return messageList.get(generalIndex).get(roundNumber);
    }

    public void setMessageValue(int generalIndex, int roundNumber, int messageIndex, String value){
        messageList.get(generalIndex).get(roundNumber).get(messageIndex).setValue(value);
    }

    public void setMessagePath(int generalIndex, int roundNumber, int messageIndex, String value){
        messageList.get(generalIndex).get(roundNumber).get(messageIndex).setPath(value);
    }

    public void setMessageCalcValue(int generalIndex, int roundNumber, int messageIndex, String value){
        messageList.get(generalIndex).get(roundNumber).get(messageIndex).setCalcValue(value);
    }

    public void RunSimulation(ArrayList<General> g)
    {
        Random gen = new Random(System.nanoTime());
        String sentMessage = Integer.toString(gen.nextInt(2));

        for(int r = 0; r < rounds; r++){
            for (int p = 0; p < numGenerals; p++) {

                // Handle round 0
                if (r == 0) {
                    if (g.get(commander).getType().equals(ByzantineModel.COMMANDER_REBEL)) {
                        sentMessage = Integer.toString(gen.nextInt(2));
                        if (p == commander) {
                            sentMessage = "X";
                        }
                    }
                    setMessageValue(p,r,0,sentMessage);
                    g.get(p).addMessage(r,  getMessageList(p,r).get(0));
                }

                // Everything else
                else {
                    for (int m = 0, general = 0; m < getMessageList(p,r).size(); m++) {
                        while (getMessageList(p,r).get(m).getPath().contains(Integer.toString(general))) {
                            general++;
                        }
                        //setMessagePath(p,r,)
                        
                    }

                }
                

            }


        }
    }

}
